<?php
	require_once realpath(dirname(__FILE__)) . '/../Configuration.php';

	require_once LIB_PATH . '/log4php/Logger.php';
	
	class DbHelper{

	   private static $instance = null;
	   private static $log = null;
	   protected $db_handle = null;
	   
	   private static $db_name = DB_LOCATION;
	   
	   /**
	    * @throws Exception
	    */
	   private function __construct(){
	   		self::$log = Logger::getLogger('DbHelper');
			try{
	   			$this->db_handle = new PDO("sqlite:".self::$db_name);
			}catch(PDOException $e){
		    	self::$log->error('Db connection error: '.$e->getMessage());
				throw new Exception($e->getMessage());
		    }
	   }
	   
	   /**
	    * Restituisce l'istanza della classe
	    * @throws Exception
	   */
	   public static function getInstance(){
	      if(self::$instance == null){   
	         self::$instance = new DbHelper();
	      }
	      
	      return self::$instance;
	   }
	   
	   function getDbHandle(){
	   		return $this->db_handle;
	   }
	}